<?php
require_once 'config21.php';
if (!isset($_SESSION['userid'])) {
    header('Location: login21.php');
    exit;
}

// 添加班级
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['add_class'])) {
    $class_name = $_POST['class_name'];
    $department_id = $_POST['department_id'];
    $sql = "INSERT INTO classes (class_name, department_id) VALUES ('$class_name', '$department_id')";
    if ($conn->query($sql) === TRUE) {
        header('Location: class_management21.php');
        exit;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html lang="zh-Hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加班级</title>
    <link rel="stylesheet" href="style.css">
    <style>
        .add-button {
            display: inline-block;
            background-color: #007bff;
            color: white;
            padding: 8px 16px;
            text-decoration: none;
            border-radius: 4px;
        }
        .add-button:hover {
            background-color: #0056d3;
        }
    </style>
</head>
<body>
    <h2>添加班级</h2>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
        <label for="class_name">班级名称：</label>
        <input type="text" name="class_name" require><br>
        <label for="department_id">所属系部：</label>
        <select name="department_id">
            <?php
            $department_sql = "SELECT * FROM departments";
            $department_result = $conn->query($department_sql);
            while ($department_row = $department_result->fetch_assoc()) {
                echo "<option value='" . $department_row['department_id'] . "'>" . $department_row['department_name'] . "</option>";
            }
            ?>
        </select><br>
        <input type="submit" value="添加" name="add_class">
    </form>
    <div align="right"><a href="class_management21.php" class="add-button">返回</a></div>
</body>
</html>